package com.guide.router;




public class SimpleHashRouter {
	
	public SimpleHashRouter(){
	}


	public static int getTableIndex(long shardingId, int cellCount) {
		if(shardingId < 0) throw new IllegalArgumentException("sharding id can not be a negative number");
		
		return cellCount > 1 ? (int)(hashLong(shardingId) % cellCount) : -1;
	}
	
	

	private static long hashLong(long hash) {
		return Math.abs(DEKHash(hash + "mykey%_@***1568"));
	}

	
	private static long DEKHash(String str) {
		long hash = str.length();
		for (int i = 0; i < str.length(); i++) {
			hash = ((hash << 5) ^ (hash >> 27)) ^ str.charAt(i);
		}
		return hash;
	}
	
	public static int getSimpleMod(long shardingId, int cellCount) {
		if(shardingId < 0) throw new IllegalArgumentException("sharding id can not be a negative number");
		return (int)shardingId % cellCount;
	}
	

}
